
import { Button, Flex, Form } from "@/components/base";
import { useNavigate } from "react-router-dom";
import * as UI from "../../ui";
import * as hooks from "./hooks";
import * as apis from "@/apis";
import React from "react";

export function OrderEdit() {

    const navigate = useNavigate();
    const [form] = Form.useForm();
    const { id } = hooks.useSearchId();
    hooks.useOrderDetails(id, form);

    const onCreate = React.useCallback(async () => {
        const values = await form.validateFields();
        await apis.order.update.post(removeEmpty({...values, id}));
        navigate(-1);
    }, [form, id, navigate]);

    return (
        <Form form={form} layout='vertical' >
            <Flex justify='flex-end' gap={10} align='center' style={{ marginBottom: 20 }} >
                <Button onClick={() => { navigate(-1) }} >返回</Button>
                <Button onClick={onCreate} type='primary' >确定</Button>
            </Flex>
            <UI.FormItem />
        </Form>
    )
}

/** 对象去空 */
function removeEmpty(obj: any) {
    const newObj:any = {};
    Object.keys(obj).forEach(key => {
        if (obj[key] !== undefined && obj[key] !== null && obj[key] !== "") {
            newObj[key] = obj[key];
        }
    });
    return newObj;
}